/*
 * @author zei, QQ:77132995, email:kazeik@163.com
 * 2022/3/7 17:11
 * 类说明:
 */

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/enums/data_import_template_type.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/provider/dashboard/contact/contact_data_import_provider.dart';
import 'package:finance_app/src/provider/dashboard/products_services/data_import_provider.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:finance_app/src/widgets/bottom_sheet/default_bottom_sheet.dart';
import 'package:finance_app/src/widgets/bottom_sheet/delete_info_bottom_sheet.dart';
import 'package:finance_app/src/widgets/bottom_sheet/got_it_tip_bottom_sheet.dart';
import 'package:finance_app/src/widgets/button/normal_text_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:quiver/strings.dart';

@RouterMeta(paramName: "contentDataImportPage")
class ContactDataImportPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _ContactDataImportPage();
}

class _ContactDataImportPage extends State<ContactDataImportPage> with PagePresenter<ContactDataImportPage, ContactDataImportProvider> {
  @override
  Widget get buildWidget => ListView(
        shrinkWrap: true,
        children: [
          _buildItemCell(S.current.download_our_template_file),
          _buildIconText(S.current.download_template_file, A.assetsImagesStyleDownlaod, click: () => provider.downloadTemplateFile()),
          _buildItemCell(S.current.fill_in_the_template),
          Container(
            padding: EdgeInsets.only(
              left: getDp(20),
              top: getDp(20),
              bottom: getDp(20),
            ),
            child: Text(
              S.current.column_template_import_work,
              style: TextStyle(
                color: aceDarkGrayColor,
                fontSize: getSp(18),
                // fontWeight: FontWeight.w700,
                height: 20 / 18,
              ),
            ),
          ),
          _buildItemCell(S.current.upload_the_filled_template_file),
          Container(
            padding: EdgeInsets.only(
              left: getDp(20),
              top: getDp(20),
              bottom: getDp(20),
            ),
            child: Text(
              S.current.supported_file_formats,
              style: TextStyle(
                color: aceDarkGrayColor,
                fontSize: getSp(18),
                height: 20 / 18,
              ),
            ),
          ),
          listBuilder<bool>(
            selector: (context, provider) => provider.importItems == null || provider.importItems.isEmpty,
            builder: (context, value, child) => value == true ? _uploadIcon() : _buildFileInfo(),
          ),
        ],
      );

  Widget _uploadIcon() => Container(
        alignment: Alignment.centerLeft,
        margin: EdgeInsets.only(left: getDp(20), right: getDp(20)),
        child: SizedBox(
          width: 100,
          height: 100,
          child: InkWell(
            onTap: () => provider.uploadTemplateFile(),
            child: Container(
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: pinBlock,
                borderRadius: BorderRadius.all(
                  Radius.circular(
                    getDp(5),
                  ),
                ),
              ),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  SvgPicture.asset(
                    A.assetsImagesStylePin,
                    fit: BoxFit.contain,
                  ),
                  Text(
                    S.current.upload,
                    style: TextStyle(
                      color: aceDarkGrayColor,
                      fontSize: getSp(18),
                      fontWeight: FontWeight.w700,
                      height: 20 / 18,
                    ),
                  )
                ],
              ),
            ),
          ),
        ),
      );

  Widget _buildItemCell(String msg, {int total = 0}) => Container(
        color: aceWhiteColor,
        padding: EdgeInsets.only(
          left: getDp(20),
          right: getDp(20),
          top: getDp(10),
          bottom: getDp(10),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              msg,
              style: TextStyle(
                color: aceDarkGrayColor,
                fontSize: getSp(18),
                fontWeight: FontWeight.w700,
                height: 20 / 18,
              ),
            ),
            listBuilder<bool>(
              selector: (context, provider) => total == null || total == 0,
              builder: (context, value, child) => value == true
                  ? Container()
                  : Text(
                      "${S.current.total}:$total",
                      style: TextStyle(
                        color: aceDarkGrayColor,
                        fontSize: getSp(18),
                        fontWeight: FontWeight.w700,
                        height: 20 / 18,
                      ),
                    ),
            ),
          ],
        ),
      );

  Widget _buildIconText(String msg, String assets, {Function click}) => Container(
        padding: EdgeInsets.all(
          getDp(20),
        ),
        child: InkWell(
          onTap: click,
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text(
                msg,
                style: TextStyle(
                  color: aceBlueColor,
                  fontSize: getSp(18),
                  fontWeight: FontWeight.w700,
                  height: 20 / 18,
                ),
              ),
              Padding(
                padding: EdgeInsets.only(
                  left: getDp(10),
                ),
                child: SvgPicture.asset(
                  assets,
                  fit: BoxFit.contain,
                ),
              )
            ],
          ),
        ),
      );

  Widget _buildFileInfo() => ListView(
    physics: NeverScrollableScrollPhysics(),
    shrinkWrap: true,
    children: [
      _buildIconText(
        isEmpty(provider.uploadFileName) ? "" : provider.uploadFileName,
        A.assetsImagesStyleDelete_1,
        click: () => showDeleteInfoBottomSheet(context, S.current.delete_uploaded_file, S.current.lose_all_file, onDelete: () {
          Navigator.pop(context);
          provider.deleteSelectFile();
        }, sureBtnText: S.current.delete, cancelText: S.current.cancel),
      ),
      _buildItemCell(S.current.summary),
      _infoItem(S.current.total_business_contact,
          "${provider.contentDatas.typesCount == null || provider.contentDatas.typesCount.customer == null ? 0 : provider.contentDatas.typesCount.customer}",
          body: true),
      _buildItemCell(S.current.data_mapped),
      _infoItem(S.current.business_contact, S.current.relationship, body: true),
      ListView.separated(
        itemCount: provider.importItems == null || provider.importItems.length == 0 ? 0 : provider.importItems.length,
        shrinkWrap: true,
        physics: NeverScrollableScrollPhysics(),
        itemBuilder: (context, index) => _infoItem(isEmpty(provider.importItems[index].fullName) ? "" : provider.importItems[index].fullName,
            isEmpty(provider.importItems[index].typeStar) ? "" : provider.importItems[index].typeStar,
            body: false),
        separatorBuilder: (context, index) => Divider(
          height: 1,
        ),
      ),
    ],
  );

  Widget _infoItem(String leftStr, String rightStr, {String centerStr = "", bool body = true}) {
    var weight = body == true ? FontWeight.w700 : FontWeight.normal;
    return Container(
      margin: EdgeInsets.only(
        top: getDp(10),
        bottom: getDp(10),
      ),
      padding: EdgeInsets.only(
        left: getDp(20),
        right: getDp(20),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Expanded(
              flex: 1,
              child: Text(
                leftStr ?? "",
                style: TextStyle(
                  color: aceBlackColor,
                  fontSize: getSp(16),
                  fontWeight: weight,
                  height: 20 / 16,
                ),
              )),
          isEmpty(centerStr)
              ? Container()
              : Expanded(
                  flex: 2,
                  child: Text(
                    centerStr ?? "",
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      color: aceBlackColor,
                      fontSize: getSp(16),
                      fontWeight: weight,
                      height: 20 / 16,
                    ),
                  ),
                ),
          Expanded(
            flex: 1,
            child: Text(
              rightStr ?? "",
              textAlign: TextAlign.right,
              maxLines: 2,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                color: aceBlackColor,
                fontSize: getSp(16),
                fontWeight: weight,
                height: 20 / 16,
              ),
            ),
          ),
        ],
      ),
    );
  }

  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(title: S.current.data_import, actions: [
        NormalNextTextButton(
          onTap: () => provider.showConfirmImport(context),
        )
      ]);
}
